import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().removeDuplicates("abbaca"));
    }
}

//不使用Stack，直接用StringBuilder
class Solution {
    public String removeDuplicates(String s) {
        StringBuilder stack = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (stack.length() > 0 && c == stack.charAt(stack.length() - 1)) {
                stack.deleteCharAt(stack.length() - 1);
            } else {
                stack.append(c);
            }
        }
        return stack.toString();
    }
}


//使用Stack
class Solution1 {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!stack.empty() && stack.peek() == c) {
                stack.pop();
            } else {
                stack.push(c);
            }
        }

        StringBuilder sb = new StringBuilder();
        for (char each : stack) {
            sb.append(each);
        }
        return sb.toString();
    }
}